In [5]:
class M(object):
def public(self):
print 'This can be seen in tab'
def _private(self):
print 'This cant be seen in tab due to _'
print '_ is a weak internel use indicator'
In [6]:
m = M()
In [7]:
m.public()
In [8]:
m._private()
In [9]:
class MyClass():
def __init__(self):
self.__superprivate = 'Hello'
self._semiprivate = 'World'
In [16]:
m = MyClass()
In [17]:
print m.__superprivate
In [18]:
print m._semiprivate
In [19]:
print m.__dict__
In [36]:
import ctypes
In [53]:
class DynamicArray(object):
def __init__(self):
self.n = 0
self.capacity = 1
self.A = self._make_array(self.capacity)
def __len__(self):
return self.n
def __getitem__(self,k):
if not 0 <= k <= self.n:
return IndexError('k is out of bounds')
return self.A[k]
def _resize(self,new_cap):
B = self._make_array(new_cap)
for k in range(self.n):
B[k] = self.A[k]
self.A = B
self.capacity = new_cap
def append(self,ele):
if self.n == self.capacity:
self._resize(2*self.capacity)
self.A[self.n] = ele
self.n += 1
def _make_array(self, new_cap):
return (new_cap*ctypes.py_object)()
In [54]:
print range(10)
In [70]:
da = DynamicArray()
In [71]:
len(da)
Out[71]:
In [72]:
da.capacity
Out[72]:
In [57]:
da.append(1)
In [58]:
len(da)
Out[58]:
In [59]:
da[0]
Out[59]:
In [60]:
da[1]
In [61]:
da.append(2)
In [62]:
da[1]
Out[62]:
In [63]:
import sys
In [75]:
da = DynamicArray()
for k in range(50):
a = len(da)
#b = sys.getsizeof(da)
b = da.capacity
print 'Length: {0:3d}; Size in bytes {1:4d}'.format(a,b)
da.append(k)